/*******************************************************************************
*  file    : application.cpp
*  created : 15.08.2013
*  author  : Slyshyk Oleksiy (alexSlyshyk@gmail.com)
*******************************************************************************/

#include "application.hpp"
#include <QTextCodec>
#include <QTimer>
#include <QDebug>
#include <QDir>
#include <qlogger.hpp>

namespace {
QLogger* l = QLoggerEngine::getLogger("core");
}

Application::Application(int &argc, char **argv
                         )
    : QCoreApplication(argc, argv)
{
    setApplicationName   (QLatin1String("modemsrv"));
    setOrganizationName  (QLatin1String("Slyshyk 0."));
    setOrganizationDomain(QLatin1String("alexSlyshyk@gmail.com"));

    setCodecs();
}
Application *Application::instance()
{
    return qobject_cast<Application *>(QCoreApplication::instance());
}
/**
 * Interrupt the application. This is directly called from a signal handler.
 */
void Application::userInterrupt()
{
    logInfo(l)<<tr("Start deinitialization of service.");
    exit(0);
}
const QString &Application::logDir()
{
    checkLogDir();
    return log_dir_;
}
const QString &Application::dataDir()
{
    checkDataDir();
    return data_dir_;
}
void Application::checkLogDir()
{
    QString path = QString("%1/%2").arg(qApp->applicationDirPath()).arg("log");
    QDir log_dir( path );
    if( log_dir.exists() )
        log_dir_ = log_dir.canonicalPath();
    else if (QDir(qApp->applicationDirPath()).mkdir("log"))
        {
            log_dir_ = QDir(path).canonicalPath();
        }
}
void Application::checkDataDir()
{
    QString path = QString("%1/%2").arg(qApp->applicationDirPath()).arg("data");
    QDir dir( path );
    if( dir.exists() )
        data_dir_ = dir.canonicalPath();
    else if (QDir(qApp->applicationDirPath()).mkdir("data"))
        {
            data_dir_ = QDir(path).canonicalPath();
        }
}
void Application::setCodecs()
{
#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
    QTextCodec::setCodecForTr    (QTextCodec::codecForName("UTF-8"));
#endif
}
